/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.iss.examsys.view;

import com.iss.examsys.ejb.ExamPaperBean;
import com.iss.examsys.ejb.ExamSessionBean;
import com.iss.examsys.ejb.UserBean;
import com.iss.examsys.entity.ExamPaper;
import com.iss.examsys.entity.ExamSession;
import com.iss.examsys.entity.StudentModule;
import com.iss.examsys.entity.User;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.Dependent;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Quek Ting Hong
 */
@Named(value = "examListView")
@RequestScoped
public class ExamListView {

    private List<ExamSession> examSessionList = new ArrayList();
    
    @EJB ExamSessionBean examSessionBean;
    @EJB UserBean userBean;
    
    public ExamListView() {           
        
    }

    public List<ExamSession> getExamSessionList() {
        
        if(examSessionList==null || examSessionList.size()==0){
            ExternalContext ctx = FacesContext.getCurrentInstance().getExternalContext();
                    HttpServletRequest req = (HttpServletRequest)ctx.getRequest();

            System.out.print(req.getRemoteUser());
            System.out.print(req.getUserPrincipal());
            System.out.print(FacesContext.getCurrentInstance().getExternalContext().getAuthType());

            User user = userBean.findByUserId(req.getRemoteUser());

            if(user != null){
                System.out.print("Student take exam : "+user.getName());

                if(user.getStudentModuleList()!=null && user.getStudentModuleList().size()>0){
                    List<Long> moduleIdList = new ArrayList();
                    List<StudentModule> modules = user.getStudentModuleList();

                    for(StudentModule m : modules){
                        moduleIdList.add(m.getId());                    
                    }

                    examSessionList = examSessionBean.findByModuleIds(moduleIdList);
                }

            }    
        }
        
        return examSessionList;
    }

    public void setExamSessionList(List<ExamSession> examSessionList) {
        this.examSessionList = examSessionList;
    }
    
}
